(function (doc, win) {
	var docEl = doc.documentElement,
			resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
			recalc = function () {
				var clientWidth = docEl.clientWidth;
				if (!clientWidth) return;
				docEl.style.fontSize = 100 * (clientWidth /375) + 'px';
			};

	if (!doc.addEventListener) return;
	win.addEventListener(resizeEvt, recalc, false);
	doc.addEventListener('DOMContentLoaded', recalc, false);
	// active兼容处理 即 伪类 :active 失效
	document.addEventListener('touchstart',function(){},false);
})( document, window);





(function(){
	wlxerCM = {
		init:function(){
			var self = this;

			//模拟checkbox事件
			self.checkbox();

			// 模拟radio事件
			self.radio();

			//筛选
			self.filter();

			//显示搜索列表
			self.search();

			//设置模版数据
			self.setModData();

			//
			self.clearInput();

			//加减
			self.addDel();
			//有数据时按钮高亮
			self.bntHightLight();

		},
		clearInput:function(){
			var again = false;

			//输入内容显示『X』	
			$('.page__bd').on('keyup focus','.weui-input,.weui-textarea',function(){
				var $this = $(this),
					$clear = $this.next('.clearText'),
					val = $this.val();
				
				$('.clearText').addClass('f-dn');

				//有数据，禁止输入
				if ( $this.attr('readonly') ) {
					return;
				}
				if ( val!='' ){
					$clear.removeClass('f-dn');
				} else {
					$clear.addClass('f-dn');
				}

			});

			//清除输入的内容
			$('.page__bd').on('click','.clearText',function(){
				var $this = $(this),
					$input = $this.prev();
				$input.val('');
				$this.addClass('f-dn');
			});

			
		},
		/**
		 * 找backData中的id
		 */
		findBackDataId:function(id){
			var backData = null,
				index = -1;


			if (localStorage.backData) {
				backData = JSON.parse(localStorage.backData);
				$.each(backData,function(i){
					if (this.id == id) {
						index = i;
						return false;
					}
				});
			}

			

			return index;
		},
		/**
		 * 设置当前的日期，返回'2015-08-12'
		 */
		getDate:function(){
			var self = this;
			var n = new Date(),
				year = n.getFullYear(),
				month = self.toTwo( n.getMonth()+1 ),
				date = self.toTwo( n.getDate() ),
				hours = self.toTwo( n.getHours() ),
				minutes = self.toTwo( n.getMinutes() );


			return [year,month,date].join('-');
		},
		/**
		 * 设置当前的时间，返回'2015-08-12 15:21'
		 */
		getTime:function(){
			var self = this;
			var n = new Date(),
				year = n.getFullYear(),
				month = self.toTwo( n.getMonth()+1 ),
				date = self.toTwo( n.getDate() ),
				hours = self.toTwo( n.getHours() ),
				minutes = self.toTwo( n.getMinutes() );


			return [year,month,date].join('-') + ' ' + hours+':'+minutes;
		},
		/**
		 * 一位数转换成两位数，前面补0
		 */
		toTwo:function(num){
			return num<10 ? '0'+num : ''+num;
		},
		/*
		 * 模拟checkbox事件
		 */
		checkbox: function () {
			var self = this;
			$('body').on('click', '.u-ipt-checkbox', function (ev) {
				var $this = $(this),
					$item = $this.closest('.u-checkboxitem'),
					isChecked = $this.is(':checked');

				if (isChecked) {
					$item.find('.i-checkbox').addClass('z-checked');
				} else {
					$item.find('.i-checkbox').removeClass('z-checked');
				}
			});
		},
		/*
		 * 模拟radio事件
		 */
		radio: function () {
			var self = this;
			$('body').on('click', '.u-ipt-radio', function (e) {
				var $this = $(this),
					$group = $this.closest('.m-radiogroup'),
					$item = $this.closest('.u-radioitem');

				$group.find('.i-radio').removeClass('z-checked');
				$item.find('.i-radio').addClass('z-checked');
			});
		},
		/*
		 * 筛选
		 */
		filter:function(){
			var self = this;
			//显示筛选列表
			$('body').on('click','.filterList .items',function(){
				var $this = $(this),
					id = $this.attr('href');
				//先隐藏二级筛选
				$('.filterItemsWrap').addClass('f-dn');
				$('.filterItemsWrapTime').addClass('f-dn');

				//显示当前的二级筛选
				
				if ( $this.hasClass('active') ) {
					$('.filterList .items').removeClass('active');
				} else {
					$this.addClass('active').siblings().removeClass('active');
					$('#'+id).removeClass('f-dn');

				}
				
			});
			
			//点击筛选列表的蒙版
			$('body').on('click','.filterItemsWrap .mask',function(){
				var $this = $(this),
					$filterItemsWrap = $this.closest('.filterItemsWrap');

				//隐藏二级筛选列表
				$filterItemsWrap.addClass('f-dn');
				$('.filterList .items').removeClass('active');
			});

			//点击二级筛选列表
			$('body').on('click','.filterItemsWrap .filterItems .item',function(){
				var $this = $(this),
					$filterItemsWrap = $this.closest('.filterItemsWrap'),
					id = $filterItemsWrap.attr('id'),
					txt = $this.text();

				$('.filterList .items[href='+ id +']').text(txt);

				//隐藏二级筛选列表
				$filterItemsWrap.addClass('f-dn');
				$('.filterList li').removeClass('active');

				
			});

			/* 下面是时间筛选的事件 */

			//点击一级的时间筛选
			$('body').on('click','.filterList .itemsTime',function(){
				var $this = $(this),
					id = $this.attr('href');

				//先隐藏二级筛选
				$('.filterItemsWrap').addClass('f-dn');
				$('.filterItemsWrapTime').addClass('f-dn');

				//显示当前的二级筛选
				
				if ( $this.hasClass('active') ) {
					$('.filterList .itemsTime').removeClass('active');
				} else {
					$this.addClass('active').siblings().removeClass('active');
					$('#'+id).removeClass('f-dn');

				}

				$('.filterTime').val( self.getTime() )
				$('.filterTime').datetimePicker();

			});

			//点击筛选列表的蒙版（时间筛选）
			$('body').on('click','.filterItemsWrapTime .mask',function(){
				var $this = $(this),
					$filterItemsWrapTime = $this.closest('.filterItemsWrapTime');

				//隐藏二级筛选列表
				$filterItemsWrapTime.addClass('f-dn');
				$('.filterList li').removeClass('active');
			});

			//时间筛选，点击确定
			$('body').on('click','.filterItemsWrapTime .btn-confirm',function(){
				var $this = $(this),
					$filterItemsWrapTime = $this.closest('.filterItemsWrapTime'),
					id = $filterItemsWrapTime.attr('id'),
					$id = $('.filterList .itemsTime[href='+ id +']'),
					txt = $filterItemsWrapTime.find('input').val();


					$id.text(txt);

					//隐藏二级筛选列表
					$filterItemsWrapTime.addClass('f-dn');
					$('.filterList li').removeClass('active');
			});

			//时间筛选，点击重置
			$('body').on('click','.filterItemsWrapTime .btn-reset',function(){
				var $this = $(this),
					$filterItemsWrapTime = $this.closest('.filterItemsWrapTime'),
					id = $filterItemsWrapTime.attr('id'),
					$id = $('.filterList .itemsTime[href='+ id +']'),
					txt = $id.attr('data-defaultText');

					$id.text(txt);

					//隐藏二级筛选列表
					$filterItemsWrapTime.addClass('f-dn');
					$('.filterList li').removeClass('active');

			});




		},
		/**
		 * 显示搜索列表
		 */
		search:function(){
			//输入内容，显示列表
			$('.weui-search-bar__input').on('keyup',function(){
				var $this = $(this),
					val = $this.val();

				//隐藏筛选的列表
				$('.filterItemsWrap').addClass('f-dn');
				$('.filterList .items').removeClass('active');


				if (val==''){
					$('.searchbar-result').addClass('f-dn');
				} else {
					$('.searchbar-result').removeClass('f-dn');
				}
				
			});
			//点击搜索的列表
			$('.searchbar-result').on('click','.weui-cell',function(){
				var $this = $(this),
					$result = $this.closest('.searchbar-result');
				$result.addClass('f-dn');
			});
			//点击搜索的关闭
			$('.weui-icon-clear').on('click',function(){
				$('.weui-search-bar__input').val('');
				$('.searchbar-result').addClass('f-dn');
			});
		},
		/**
		 * 获取当前页面的url
		 * @return {[string]} [index.html]
		 */
		getUrl:function(){
			var arr = location.pathname.split('/');
			return location.pathname.split('/')[arr.length-1];
		},
		/**
		 * 获取当前路径
		 * 如：location 的路径：http://localhost:3000/dist/project/mod_input.html
		 * 返回：http://localhost:3000/dist/project
		 */
		getPath:function(){
			var arr = location.href.split('/');
			arr.pop();
			return arr.join('/')+'/';

		},
		/**
		 * 设置模版数据
		 */
		setModData:function(){
			var self = this;
			$('.page__bd').on('click','.setModData',function(){
				var $this = $(this),
					type = $this.attr('data-type'),
					url = self.getUrl(),
					title = $this.attr('data-title'),
					id = $this.attr('data-id'),
					con = $this.attr('data-con'),
					unit = $this.attr('data-unit'),
					sourceData = {
						type:type,
						backUrl : url,
						title : title,
						id : id,
						con : con,
						unit : unit
					},
					sourceDataStr = JSON.stringify(sourceData);

				localStorage.sourceData = '';
				localStorage.sourceData = sourceDataStr;


				location.href = self.getPath() + 'mod_'+ type + '.html';
			})
		},
		/**
		 * 加减
		 */
		addDel: function () {
			// 点击减号
			$('.m-addDelWrap').on('touchend', '.f-del', function (ev) {
				var $this = $(this),
					$addDelWrap = $(ev.delegateTarget)
				numVal = parseInt($('.u-ipt-num', $addDelWrap).val());

				if ( !$this.hasClass('disabled') ) {
					$('.u-ipt-num', $addDelWrap).val(--numVal);
					if ( numVal == 1) {
						$this.addClass('disabled');
					}
					
				};
				return false;

			});
			//点击加号
			$('.m-addDelWrap').on('touchend', '.f-add', function (ev) {
				var $this = $(this),
					$addDelWrap = $(ev.delegateTarget)
				numVal = parseInt($('.u-ipt-num', $addDelWrap).val());

				$('.u-ipt-num', $addDelWrap).val(++numVal);
				$('.f-del', $addDelWrap).removeClass('disabled');

			});
		},
		//有数据时按钮高亮
		bntHightLight:function(){
			$('.bntHightLight').on('keyup focus input',function(){
				var whithSpa = false;
				$('.bntHightLight').each(function(){
					if($(this).val()==''){
						whithSpa = true;
					}
				})
					if(whithSpa){
						$('.lightBtn').removeClass('hightLightBtn');				
					}else{
						$('.lightBtn').addClass('hightLightBtn');
					}
				
			});
		}
	}
})()


$(function(){
	wlxerCM.init();
})